<?php
	/**
	 * 领取奖品
	 */
    class Receiveprize extends APIBase {
		
		public $tag = "Receiveprize";
    	public $isLogin = true;
		
		public function before() {
			$this->initCacheRedis();
			$this->initMysql();
			return true;
		}
		public function logic() {
			//$ann = $this->cache_redis->hMGet("ha:*", array("id","title","content","image_url"));
			//邮件ID
			$ID = $this->param['id'];
			//获取用户信息
			$uid = $this->uid;
			$userinfo = $this->data_redis->hmGet("hu:{$uid}",array("room_card","room_card_coop","coin","money","is_vip"));
            
			//获取邮件列表
			$hUserEmailList = $this->cache_redis->keys("hUserEmailList:$uid:*");
			

			$count = count($hUserEmailList);

			if ($count) {
				$room_card = (int)$userinfo['room_card'];
				$room_card_coop = (int)$userinfo['room_card_coop'];
				$coin = (int)$userinfo['coin'];
				$money = (int)$userinfo['money'];
				$is_vip = (int)$userinfo['is_vip'];
				for($i=0;$i<$count;$i++){				  
				    $list = $this->cache_redis->hGetAll("hUserEmailList:$uid:".$i); 
                    if($list['id']==$ID && $list['receivestatus']==0){
						if($list['type']==1){//排行榜
								$boardreward = $this->cache_redis->hGetAll("hboardreward:".$list['rewardid']);
								$rewardjson = $boardreward['reward'];
							}elseif($list['type']==2){//签到
								$signreward = $this->cache_redis->hGetAll("hsignreward:".$list['rewardid']);
								if($is_vip){
									$arr = json_decode($signreward['reward'],true);
									foreach($arr as $k=>$v){
										$signreward['reward'] = array($k=>(int)$v*2);
									}
									$rewardjson = json_encode($signreward['reward']);
								}else{
									$rewardjson = $signreward['reward'];
								}
								
							}elseif($list['type']==3){//抽奖
								$rewardpool = $this->cache_redis->hGetAll("hrewardpool:".$list['rewardid']);
								$rewardjson = $rewardpool['reward'];
							}elseif($list['type']==4){//分享
								$sharereward = $this->cache_redis->hGetAll("hsharereward:".$list['rewardid']);
								$rewardjson = $sharereward['reward'];
						    }elseif($list['type']==5){//首次被邀请
								$sharereward = $this->cache_redis->hGetAll("hsharereward:".$list['rewardid']);
								$rewardjson = $sharereward['reward'];
						    }
							
					    $reward = json_decode($rewardjson,true);
					    $rewardArr = array();
				        foreach($reward as $key=>$val){
						    $proplist = $this->cache_redis->hGetAll("prop:".$key);
						    if((int)$proplist['id']==1){
					        //奖励独立型房卡
						        $room_card = (int)$userinfo['room_card']+(int)$val;
						        $this->data_redis->hMset("hu:{$uid}", array('room_card'=>$room_card));								
						    }elseif((int)$proplist['id']==2){
						    //奖励共付型房卡	
						        $room_card_coop = (int)$userinfo['room_card_coop']+(int)$val;
						        $this->data_redis->hMset("hu:{$uid}", array('room_card_coop'=>$room_card_coop));
						    }elseif((int)$proplist['id']==3){
						    //奖励钻石	
						        $coin = (int)$userinfo['coin']+(int)$val;
						        $this->data_redis->hMset("hu:{$uid}", array('coin'=>$coin));
						    }elseif((int)$proplist['id']==13){
						    //奖励金币	
						        $money = (int)$userinfo['money']+(int)$val;
								//记录金币变化
					        	$logdata =  array(
				                    'type'=>$list['type'],
				                    'vid' => 0,
				                    'tid'=>  0,
				                    'seatid'=> 0,
				                    'uid' => $uid,
				                    'change_money' => $val,
									'money'=>(int)$userinfo['money']+$val,
				                    'create_time' => time(),
			                    );
                                $this->addChangeMongeyLog($logdata);	
						        $this->data_redis->hMset("hu:{$uid}", array('money'=>$money));
						    }
					    }
						$time = time();
                        //$this->mysql->query("update mail_send set is_receive = 1,is_read = 1,update_date=$time where id = $ID");	
                        $this->cache_redis->hMset("hUserEmailList:$uid:".$i, array('receivestatus'=>1,'readstatus'=>1,'update_date'=>$time));	
                           				
				    }		  
				}
	            $data = array("room_card"=>$room_card,/*"room_card_coop"=>$room_card_coop,*/"coin"=>$coin,"money"=>$money);
				$this->data_redis->hMset("hu:{$uid}", array('room_card'=>$room_card,/*'room_card_coop'=>$room_card_coop,*/'coin'=>$coin,"money"=>$money));
				$this->returnData($data);
			}else{
				$this->returnError(301,"用户无邮件可领");
			}
		}
		
        public function after() {
			$this->deinitCacheRedis();
		}
    }
?>
